#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using Gizmox.WebGUI.Common.Resources;
using TVC.Layout;
using System.Xml;
using System.IO;

#endregion

namespace TVC.Modules.Act
{
    public partial class MMALO : ModuleParent, IHostedApplication
    {
        int idCommit = 231;
        private GridLayout layout;
        private DTO.PODInfo PODInfo;

        public MMALO()
        {
            InitializeComponent();
            _dgvList = dgvList;
            _dgvFilter = gridFilter1;
            _strType = "MMA";
            _strTableName = "TVC_MFMOV";
            PODInfo = (DTO.PODInfo)Session["USER"];
            _dgvFilter.GridFiterEvent += new CustomControl.GridFilter.GridFilterHandle(_dgvFilter_GridFiterEvent);
            _isCheck = true;            
        }

        void _dgvFilter_GridFiterEvent(string strFilter, int year)
        {
            string sErr = "";
            BUS.TVC_MFMOVControl MMQCtr = new BUS.TVC_MFMOVControl(GetMainForm().PODInfo.Business_ID);
            if (strFilter != "") strFilter += " AND InTransit <> '' AND NOT(InTransit IS NULL) AND [TYPE] like 'IG%' ";
            else strFilter = " InTransit <> '' AND NOT(InTransit IS NULL) AND [TYPE] like 'IG%' ";
            DataTable dt = MMQCtr.GetbyFilter(GetMainForm().SUNDB, strFilter, ref sErr);
            dt.Columns.Add(new DataColumn(MainForm.prefixCol + "Select", typeof(bool))
            {
                DefaultValue = false
            });
            if (!string.IsNullOrEmpty(gridFilter1.SortField))
            {
                dt.DefaultView.Sort = gridFilter1.SortField + " " + gridFilter1.Direction;
            }
            bisMMALO.DataSource = dt;
        }

        public void InitializeApplication()
        {
        }

        //public void AddNew()
        //{

        //}

        public void SetObject(object obj)
        {
        }

        public HostedToolBarElement[] GetToolBarElements()
        {
            ArrayList objElements = new ArrayList();
           
            if (IsPermis(idCommit))
                objElements.Add(new HostedToolBarButton("Update in Transit", "Update in Transit", new IconResourceHandle("Approve.png"), "I"));
            objElements.Add(new HostedToolBarButton("Refresh", new IconResourceHandle("Refresh.png"), "R"));

            return (HostedToolBarElement[])objElements.ToArray(typeof(HostedToolBarElement));
        }

        public void OnToolBarButtonClick(HostedToolBarButton objButton, EventArgs objEvent)
        {
            try
            {
                sErr = "";
                string strAction = (string)objButton.Tag;


                switch (strAction)
                {
                    case "I":
                        inTransit();
                        break;

                }
                _processStatus = strAction;
                if (sErr == "") ThrowMsg("Command: " + _processStatus);
            }
            catch (Exception ex)
            {
                ThrowMsg(ex.Message + Environment.NewLine + ex.Source);
            }
        }

        private void inTransit()
        {
            sErr = "";
            gridFilter1.Focus();
            dgvList.Update();
            DataTable dt = bisMMALO.DataSource as DataTable;
            DataRow[] arrRow = dt.Select(MainForm.prefixCol + "Select='True'");
            if (arrRow.Length > 0)
            {
                if (dtpConfirmDate.Text == "" || dtpConfirmDate.Text == null)
                {
                    MessageBox.Show(" Please input ConfirmDate ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, false);
                    return;
                }
                MessageBox.Show(" Would you like to update in-transit status for these records ?", " Update ", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, delegate(object newSender, EventArgs newArgs)
                {
                    if (((MessageBoxWindow)newSender).DialogResult == DialogResult.OK)
                    {
                        BUS.TVC_MFMOVControl MMQCtr = new BUS.TVC_MFMOVControl(GetMainForm().PODInfo.Business_ID);

                        foreach (DataRow r in arrRow)
                        {
                            sErr = MMQCtr.UpdateTransit(GetMainForm().SUNDB, r["Code"].ToString(), dtpConfirmDate.Value, "");
                            if (sErr == "")
                            { dt.Rows.Remove(r); }
                        }
                        dgvList.Update();
                        mnuCheckAll.Checked = false;
                        mnuCheckCurrentPage.Checked = false;
                    }
                },false);
            }
        }

        private void dgvList_MenuClick(object objSource, MenuItemEventArgs objArgs)
        {
            if (objArgs.MenuItem.Tag.ToString().ToUpper() == "ALL")
            {
                objArgs.MenuItem.Checked = !objArgs.MenuItem.Checked;
                for (int i = 0; i < dgvList.Rows.Count; i++)
                {
                    dgvList.Rows[i].Cells[MainForm.prefixCol + "Select"].Value = objArgs.MenuItem.Checked;
                }
            }
            else if (objArgs.MenuItem.Tag.ToString().ToUpper() == "PAGE")
            {
                objArgs.MenuItem.Checked = !objArgs.MenuItem.Checked;
                int firstRow = dgvList.FirstDisplayedScrollingRowIndex;
                int Cout = dgvList.FirstDisplayedScrollingRowIndex + dgvList.ItemsPerPage;
                if (Cout > dgvList.Rows.Count) Cout = dgvList.Rows.Count;
                for (int i = firstRow; i < Cout; i++)
                {
                    dgvList.Rows[i].Cells[MainForm.prefixCol + "Select"].Value = objArgs.MenuItem.Checked;
                }
            }            
        }

        private void MMALO_Load(object sender, EventArgs e)
        {

        }
    }
}